using UnityEngine;
using System.Collections;
using System;

public class Cliente : MonoBehaviour 
{

	string msg = "Desconectado";
	public int logado;
	public string nome;
	Brodcast a = new Brodcast();
	
	void OnGUI()
	{
 		if(Network.peerType == NetworkPeerType.Disconnected)
		{
  			msg = "Sem conexao";
  		
			if(GUI.Button(new Rect(10,35,100,30),"Conectar"))
			{
				try
				{
  	 				Network.Connect("127.0.0.1",25000);
					logado = 0;
					a.logado = 0;
  				}
 						
 				catch(Exception e)
 				{
 					Debug.Log(e.ToString());
 				}
			}
 		}
 
 		if(Network.peerType != NetworkPeerType.Disconnected && logado == 0)
		{
  			msg = GUI.TextField(new Rect(10,10,300,20),msg); 
  
			if(GUI.Button(new Rect(10,35,100,30),"Login"))
			{
   				nome = msg;
				
				try
				{
					networkView.RPC("EcoSV",RPCMode.Server,msg);
					logado = 1;
					a.nome = nome;
					a.logado = 1;
				}
				
				catch(Exception e)
				{
					Debug.Log(e.ToString());
				}
  			}
 		} 
		
		if(Network.peerType != NetworkPeerType.Disconnected && logado == 1)
		{
  			msg = GUI.TextField(new Rect(10,10,300,20),msg); 
  
			if(GUI.Button(new Rect(10,35,100,30),"Sair"))
			{
   				Network.Disconnect();
				logado = 0;
				a.logado = 0;
  			}
 		} 
	}
	
	[RPC]	
	void EcoCL(String recebido)
	{
		msg = recebido;
	}
	
}

